package com.example.ffepeg.ffmTestIng;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class deleteFile {
    //删除多个文件以及当前文件夹      不安全
    public static void main(String[] args) {
        String path = "https://pushimg.liecaizhijia.com/temporary_voic/2025-02-20/ce35e78f-b673-4316-b8de-eff16ecaa7f5.wav";
        System.err.println(extractFileNameFromURL(path));
    }


    public static String extractFileNameFromURL(String urlString) {  //获取  image_8872b3f0-2ec7-42f9-9968-03481632a117.jpeg
        try {
            URL url = new URL(urlString);
            String path = url.getPath();
            String[] pathParts = path.split("/");
            return pathParts[pathParts.length - 1];
        } catch (MalformedURLException e) {
            System.out.println("Invalid URL: " + e.getMessage());
            return e.getMessage();
        }
    }

    public static void time33(){
        String path = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\imgMP4out.mp4";
        // 目标文件夹的路径
        File directory = new File(path);

        // 检查文件夹是否存在
        if (!directory.exists()) {
            System.out.println("文件夹不存在！");
            return; // 文件夹不存在，退出程序
        }

        // 调用deleteDirectory方法删除文件夹及其内容
        if (deleteDirectory(directory)) {
            System.out.println("文件夹已成功删除！");
        } else {
            System.out.println("文件夹删除失败！");
        }
    }

    public static boolean deleteDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteDirectory(file);
                }
            }
        }
        return directory.delete();
    }

    public static void TestList(List<String> list){
        if (!Objects.requireNonNull(list).isEmpty()){
            System.out.println(list);
        }else {
            System.out.println("没有参数");
        }
    }

    public static void deleteFileContent(String path) {
        File filePath = new File(path);
        // 检查文件是否存在且是一个文件而不是目录
        if (filePath.exists() && filePath.isFile()) {
            boolean isDeleted = filePath.delete();
            if (isDeleted) {
                System.out.println("文件删除成功");
            } else {
                System.out.println("文件删除失败");
            }
        } else if (!filePath.exists()) {
            System.out.println("文件不存在");
        } else {
            System.out.println("无法删除，指定路径是一个文件夹而非文件");
        }
    }


}
